package fact.view.controller;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import fact.view.DTO.MakeDTO;
import fact.view.model.MakeModelBean;
import fact.view.util.FacesUtils;
import fact.view.util.ServletUtil;


@ManagedBean(name = "makeBean")
@RequestScoped
public class MakeBean extends ServletUtil implements Serializable {

	private static final long serialVersionUID = 8292853620260831727L;
	private boolean opened = false;
	private boolean editOpened = false;
	
	
	@ManagedProperty(value="#{makeModelBean}")
	private MakeModelBean makeModelBean;
	
	ArrayList<MakeDTO> makeList=new  ArrayList<MakeDTO>();
	private SelectItem[] makeSelectList;

	private MakeDTO make=new MakeDTO();
	private MakeDTO editMake=new MakeDTO();
		 
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		
	public String addMake(ActionEvent event) throws SQLException
	{
		editOpened=false;
		opened=false;
		
		if(this.make.getMakeName()==null || this.make.getMakeName().trim().equalsIgnoreCase(""))
		{
			opened=true;
			
			FacesUtils.addErrorMessage("form:makeName","Make Name cannot be empty.");
		}
		if(makeModelBean.getMake(this.make.getMakeName().trim())!=null)
		{
			opened=true;
			
			FacesUtils.addErrorMessage("form:makeName","Make Name Already Exist.");
		}
		
		if(opened==false)
		{
			if(makeModelBean.addMake(this.make.getMakeName())==true)
			{
				makeList=makeModelBean.getMakeList();
				session.setAttribute("session_makeList", makeList);
				
				FacesUtils.addInfoMessage(null,"Make has successfully been added.");

			}
			return "admin/make.jsf";
		}
		return null;
	}
	
	public String deleteMake() throws SQLException
	{
		System.out.println("Make Id :"+this.make.getMakeId());
		if(makeModelBean.deleteMake(this.make.getMakeId())==true)
		{
			makeList=makeModelBean.getMakeList();
			session.setAttribute("session_makeList", makeList);
			
			FacesUtils.addInfoMessage(null,"Make has successfully been deleted.");
		}
		return "admin/make.jsf";
	}
	
	 
	public String editMake() throws SQLException
	{
		editOpened=false;
		opened=false;
		
		if(this.editMake.getMakeName()==null || this.editMake.getMakeName().trim().equalsIgnoreCase(""))
		{
			editOpened=true;
			
			FacesUtils.addErrorMessage("form:editMakeName","Make Name cannot be empty.");
		}
		
		if(editOpened==false)
		{
			System.out.println("Make Id :"+this.editMake.getMakeId());
			if(makeModelBean.editMake(this.editMake.getMakeName(),this.editMake.getMakeId())==true)
			{
				makeList=makeModelBean.getMakeList();
				session.setAttribute("session_makeList", makeList);
				
				FacesUtils.addInfoMessage(null,"Make has successfully been edited.");
			}
			return "admin/make.jsf";
			
		}
		return null;
		
	}



	public MakeModelBean getMakeModelBean() {
		return makeModelBean;
	}

	public void setMakeModelBean(MakeModelBean makeModelBean) {
		this.makeModelBean = makeModelBean;
	}

	public ArrayList<MakeDTO> getMakeList() {
		try{
			
			if(session.getAttribute("session_makeList")==null)
			{
				makeList=makeModelBean.getMakeList();
				session.setAttribute("session_makeList", makeList);
			}
			else
				makeList=(ArrayList<MakeDTO>)session.getAttribute("session_makeList");
		   }
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return makeList;
	}
	public void setMakeList(ArrayList<MakeDTO> makeList) {
		this.makeList = makeList;
	}

	
	public MakeDTO getMake() {
		return make;
	}

	public void setMake(MakeDTO make) {
		this.make = make;
	}

	public MakeDTO getEditMake() {
		return editMake;
	}

	public void setEditMake(MakeDTO editMake) {
		this.editMake = editMake;
	}

	
	public SelectItem[] getMakeSelectList() {
		
		if(makeList.size()==0)
			getMakeList();
		
		this.makeSelectList = new SelectItem[makeList.size()];
		 for(int i=0;i<makeList.size();i++)
			 makeSelectList[i] = new SelectItem(makeList.get(i).getMakeId(), makeList.get(i).getMakeName());
		
		 
		return makeSelectList;
	}

	public void setMakeSelectList(SelectItem[] makeSelectList) {
		
		 
		 this.makeSelectList = makeSelectList;
	}

	/** -- Popup Modal Event ---**/
		public boolean isOpened() { return opened; }
		public boolean isEditOpened() { return editOpened; }
	    
	    public void setOpened(boolean opened) { this.opened = opened; }
	    public void setEditOpened(boolean editOpened) { this.editOpened = editOpened; }
	    
	    
	    public void openEvent(ActionEvent event) {
	    	this.make=null;
	        opened = true;
	        editOpened = false;
	    }
	    public void openEditEvent(ActionEvent event)  throws SQLException{
	    	this.editMake=makeModelBean.getMake(this.editMake.getMakeId());
	    	editOpened = true;
	    	opened = false;
	    }
	    
	    
	    public void closeEvent(ActionEvent event) {
	        opened = false;
	        editOpened = false;
	    }

    /*  End of Modal */
	

}
